{% extends "manage.html" %}
{% load custom_filters %}
{% load static %}

{% block title %} Grade User {% endblock %}

{% block pagetitle %} Grade User {% endblock pagetitle %}

{% block script %}
<script type="text/javascript" src="{% static 'yaksh/js/jquery.tablesorter.min.js' %}">
</script>
<script type="text/javascript" src="{% static 'yaksh/js/mathjax/MathJax.js' %}?config=TeX-MML-AM_CHTML"></script>
<script type="text/javascript">
$(document).ready(function()
{
  $("#marks_table").tablesorter({sortList: [[2,1]]});
});

</script>
{% endblock script %}

{% block content %}
<div class="container">
{% if objects %}
  {% include "yaksh/paginator.html" %}
    <div id="accordion">
      {% for course in objects %}
          <div class="card">
              <div class="card-header">
                  <div class="row">
                      <div class="col-md-9">
                          <h5 data-toggle="tooltip" title="{{course.name}}">
                              {{ course.name }}
                          </h5>
                      </div>
                      <div class="col-md">
                          <a class="card-link btn btn-info" data-toggle="collapse" href="#collapse{{course.id}}">
                              Details
                              <i class="fa fa-toggle-down" id="toggle_course_{{course.id}}"></i>
                          </a>
                      </div>
                  </div>
              </div>
              <div id="collapse{{course.id}}" class="collapse hide" data-parent="#accordion">
                  <div class="card-body">
                      {% with course.get_quizzes as quizzes %}
                          {% if quizzes %}
                              <ul class="list-group">
                                {% for quiz in quizzes %}
                                  <li class="list-group-item">
                                    <div class="row">
                                      <div class="col-md-8">
                                        {{quiz.description}}
                                      </div>
                                      <div class="col-md-2">
                                        <a href="{% url 'yaksh:grade_user' quiz.id course.id%}" class="btn btn-primary">
                                          Grade
                                        </a>
                                      </div>
                                    </div>
                                  </li>
                                {% endfor %}
                              </ul>
                          {% else %}
                              <center>
                                  <p class="badge badge-danger badge-pill">
                                      No Quizzes
                                  </p>
                              </center>
                          {% endif %}
                      {% endwith %}
                  </div>
              </div>
          </div>
          <br>
      {% endfor %}
    </div>
  {% include "yaksh/paginator.html" %}
{% elif msg == 'grade' and not objects %}
  <br>
  <div class="alert alert-info">
    <center><h3>No courses found to grade</h3></center>
  </div>
{% endif %}
</div>
<div class="container-fluid">
  <div class="row">
    {% if status == "grade" %}
      {% if users %}
        <div id="student" class="col-md-3">
          <ul class="nav nav-pills list-group">
            {% for user in users %}
              <li class="nav-item">
                <a href="{% url 'yaksh:grade_user' quiz_id user.user__id course_id %}" class="list-group-item{% if user.user__id == data.user.id %} active {% endif %}">
                  {{user.user__first_name}} {{user.user__last_name}}
                </a>
              </li>
            {% endfor %}
          </ul>
        </div>
      {% else %}
        <div class="col-md-1"></div>
        <div class="col-md-10">
          <div class="alert alert-warning">
            <center>
              <h4>No Users Found for {{ quiz.description }}</h4>
            </center>
          </div>
        </div>
      {% endif %}
    {% endif %}

    <div id="paper" class="col-md-9">
      {% if has_quiz_assignments %}
        <a href="{% url 'yaksh:download_quiz_assignment' quiz_id course_id %}" class="btn btn-outline-info">
          <i class="fa fa-download"></i>&nbsp;Download All Assignments
        </a>
        <br><br>
      {% endif %}

      {% if messages %}
        {% for message in messages %}
        <div class="alert alert-dismissible alert-{{ message.tags }}">
            <button type="button" class="close" data-dismiss="alert">
                <i class="fa fa-close"></i>
            </button>
            <strong>{{ message }}</strong>
        </div>
        {% endfor %}
      {% endif %}

      {% if data %}
        <div class="card">
          <div class="card-header">
            Student Details
          </div>
          <div class="card-body">
              <p><b>Name:</b> {{ data.user.get_full_name.title }}</p>
              {% with data.profile as profile %}
                {% if profile %}
                <div class="row">
                  <div class="col-md-4">
                    <p><b> Roll number:</b> {{ profile.roll_number }}</p>
                    <p><b>Position: </b> {{ profile.position }}</p>
                  </div>
                  <div class="col-md-7">
                    <p><b>Department: </b>{{ profile.department }}</p>
                    <p><b>Institute: </b>{{ profile.institute }}</p>
                  </div>
                </div>
                {% endif %}
              {% endwith %}
          </div>
        </div>
        <hr>
        {% if data.papers %}
          {% with data.papers.0 as paper %}
            <div class="card">
              <div class="card-header">
                Course Details
              </div>
              <div class="card-body">
                <div class="row">
                  <div class="col-md-6">
                    <p><b>Course:</b> {{ paper.course.name }}</p>
                    <p><b>Quiz:</b> {{ paper.question_paper.quiz.description }}</p>
                  </div>
                  <div class="col-md-6">
                    <p><b>Start time:</b> {{ paper.start_time }}</p>
                    <p><b>End time:</b> {{ paper.end_time }}</p>
                  </div>
                </div>
              </div>
            </div>
          {% endwith %}
          {% for paper in data.papers %}
            <hr>
            <div class="card">
              <div class="card-header">
                Scorecard
              </div>
              <div class="card-body">
                <div class="row">
                  <div class="col-md-4">
                    <p><b>Status:</b>
                    {% if paper.passed %}
                      <span class="badge badge-pill badge-success"> Passed </span>
                    {% else %}
                      <span class="badge badge-pill badge-danger"> Failed </span>
                    {% endif %}
                    </p>
                    <p><b>Total Marks:</b> {{ paper.question_paper.total_marks }}</p>
                    <p><b>Marks obtained:</b> {{ paper.marks_obtained }}</p>
                  </div>
                  <div class="col-md-4">
                    {% if paper.percent %}
                      <p><b>Percentage obtained:</b> {{paper.percent}}%</p>
                    {% endif %}
                    <p><b>Total attempts at questions:</b> {{ paper.answers.count }}</p>
                    <p>
                      <b>Attempt Number:</b>&nbsp;
                      <span class="badge badge-pill badge-info">
                        {{paper.attempt_number}}
                      </span>
                      <select id="attempt" onchange="window.location.href=this.value" class="custom-select">
                        <option selected="">Select attempt number</option>
                        {% for attempt in attempts %}
                          <option value="{% url 'yaksh:grade_user' quiz_id user_id attempt.attempt_number course_id %}">
                        {{attempt.attempt_number}}
                        </option>
                        {% endfor %}
                      </select>
                    </p>
                  </div>
                </div>
              </div>
            </div>
            <hr>
            {% if paper.answers.count %}
            <div class="card">
              <div class="card-header">
                Submission Details
              </div>
              <div class="card-body">
                <table class="tablesorter table table-striped table-bordered table-responsive-sm" id='marks_table'>
                  <thead>
                    <tr>
                    <th>Questions</th>
                    <th>Type</th>
                    <th>Marks Obtained</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for question, answers in paper.get_question_answers.items %}
                      {% with answers|last as answer %}
                          <tr>
                          <td>
                            <a href="#question_{{question.id}}">
                              {{ question.summary }}
                            </a>
                          </td>
                          <td>{{ question.type }}</td>
                          <td>{{ answer.answer.marks }}</td>
                          </tr>
                      {% endwith %}
                    {% endfor %}
                    </tbody>
                </table>
                <form name=frm id="q{{ paper.quiz.id }}_form"
                  action="{% url 'yaksh:grade_user' quiz_id user_id paper.attempt_number course_id %}"
                  method="post">
                  {% csrf_token %}
                  {% for question, answers in paper.get_question_answers.items %}
                    <div class="card" id="question_{{question.id}}">
                      <div class="card-header text-white bg-info">
                        <strong>
                          Details: {{forloop.counter}}. {{ question.summary }}
                          <span class="marks pull-right"> Mark(s): {{ question.points }} </span>
                        </strong>
                      </div>
                      <div class="card-body">
                        <h5>
                          <span class="badge badge-pill badge-primary">Question:</span>
                        </h5>
                        <strong>{{ question.description|safe }}</strong>
                        <br><br>
                        {% if question.type == "mcq" or question.type == "mcc" %}
                          <h5>
                            <span class="badge badge-pill badge-primary">Choices:</span>
                          </h5>
                          {% for testcase in question.get_test_cases %}
                            {% if testcase.correct %}
                              <strong>
                                <span class="badge badge-pill badge-success">
                                  {{ forloop.counter }}.
                                </span>
                                {{ testcase.options|safe }}
                              </strong>
                              {% else %}
                              <strong>
                                <span class="badge badge-pill badge-secondary">
                                  {{ forloop.counter }}.
                                </span>
                                {{ testcase.options|safe }}
                              </strong>
                            {% endif %}
                            <br>
                          {% endfor %}
                        {% elif question.type == "integer" or question.type == "string" or question.type == "float" %}
                          <h5>
                            <span class="badge badge-pill badge-primary">
                              Correct Answer:
                            </span>
                          </h5>
                          {% for testcase in question.get_test_cases %}
                            <strong>{{ testcase.correct }}</strong>
                            {% if testcase.error_margin %}
                            <strong>{{ testcase.error_margin }}</strong>
                            {% endif %}
                          {% endfor %}
                        {% elif question.type == "arrange" %}
                          <h5>
                            <span class="badge badge-pill badge-primary">
                            Correct Order:</span>
                          </h5>
                          <div class="list-group" >
                          {% for testcase in question.get_test_cases %}
                              <li class="list-group-item">
                                <strong>{{ testcase.options }}</strong>
                              </li>
                          {% endfor %}
                          </div>
                        {% else %}
                          <h5>
                            <span class="badge badge-pill badge-primary">Test cases:
                            </span>
                          </h5>
                          {% for testcase in question.get_test_cases %}
                              <strong>
                                {{ forloop.counter }}. {{ testcase }}
                              </strong>
                              <br>
                          {% endfor %}
                        {% endif %}
                        <br>
                        <h5>
                          <span class="badge badge-pill badge-primary">Student answer(s):
                          </span>
                        </h5>
                        {% if question.type == "upload" %}
                          {% if has_user_assignments %}
                            <a href="{% url 'yaksh:download_user_assignment' question.id data.user.id paper.question_paper.quiz.id course_id %}">
                            <div class="btn btn-outline-info text-center">
                            Assignment File for {{ data.user.get_full_name.title }}
                            </div>
                            </a>
                          {% else %}
                            <center>
                              <div class="alert alert-warning">
                                <h5>
                                  Assignment not submitted by {{ data.user.get_full_name.title }}
                                </h5>
                              </div>
                            </center>
                          {% endif %} <!-- End has_user_assignments -->
                        {% else %}
                        {% for ans in answers %}
                          <strong>
                            Attempt Number: {{forloop.counter}}
                          </strong>
                          <div id="accordian">
                            <div class="card">
                              {% if ans.answer.correct %}
                                <div class="card-header">
                                  <span class="badge badge-success">
                                    Correct answer:
                                  </span>
                                  <a class="card-link" data-toggle="collapse" href="#submitted_{{ans.answer.id}}">
                                    <span class="pull-right">
                                      Details&nbsp;<i class="fa fa-toggle-down"></i>
                                    </span>
                                  </a>
                                </div>
                              {% else %}
                                <div class="card-header">
                                  <span class="badge badge-danger">
                                    Error:
                                  </span>
                                  <a class="card-link" data-toggle="collapse" href="#submitted_{{ans.answer.id}}">
                                    <span class="pull-right">
                                      Details&nbsp;<i class="fa fa-toggle-down"></i>
                                    </span>
                                  </a>
                                </div>
                              {% endif %}
                              <div class="collapse hide" id="submitted_{{ans.answer.id}}" data-parent="#accordion">
                                <div class="card-body">
                                  {% with ans.error_list as err %}
                                    {% for error in err %}
                                      {% if error.type == 'stdio' %}
                                        {% if error.given_input %}
                                          <table class="table table-bordered table-responsive-sm">
                                            <tr class="table-active">
                                            <td> For given Input value(s):</td>
                                            <td>{{error.given_input}}</td>
                                            </tr>
                                          </table>
                                        {% endif %}
                                        <table id="course-detail" class="table table-bordered table-responsive-sm" width="100%" id="output">
                                          <tr>
                                          <th><center>Line No.</center></th>
                                          <th><center>Expected Output</center></th>
                                          <th><center>User output</center></th>
                                          <th><center>Status</center></th>
                                          </tr>
                                          {% for expected,user in error.expected_output|zip:error.user_output %}
                                          <td> {{forloop.counter}} </td>
                                          <td>{{expected|default:""}} </td>
                                          <td>{{user|default:""}}</td>
                                          {% if forloop.counter0 in error.error_line_numbers or not expected or not user %}
                                          <td><span class ="fa fa-times text-warning"/></td>
                                          {% else %}
                                          <td><span class ="fa fa-check text-success"/></td>
                                          {% endif %}
                                          </tr>
                                          {% endfor %}
                                          </table>
                                          <table width="100%" class="table table-bordered table-responsive-sm">
                                            <col width="10">
                                            <tr>
                                              <td>Error:</td>
                                              <td>{{error.error_msg}}</td>
                                            </tr>
                                        </table>
                                      {% elif error.type == 'assertion' %}
                                        {% if error.test_case %}
                                          <strong>
                                            We tried you code with the following test case:
                                          </strong><br><br>
                                          <pre><code>
                                            <strong>{{error.test_case}}</strong>
                                          </code></pre>
                                        {% endif %}
                                        <p><b>The following error took place: </b></p>
                                        <table id="course-detail" class="table table-bordered table-responsive-sm" width="100%">
                                            <tr class = "active">
                                            <td><b>Exception Name: </b></td>
                                            <td><span>{{error.exception}}</span></td>
                                          </tr>
                                          <tr>
                                            <td><b>Exception Message: </b></td><td>{{error.message}}</td>
                                          </tr>
                                          <tr>
                                            {% if error.traceback %}
                                            <td><b>Full Traceback: </b></td>
                                            <td><pre>{{error.traceback}}</pre></td>
                                            {% endif %}
                                          </tr>
                                        </table>
                                      {% else %}
                                        <pre><code> {{error}} </code></pre>
                                      {% endif %}
                                    {% endfor %}
                                  {% endwith %}
                                  {% if question.type == "code" %}
                                  {% pygmentise_user_answer question.language ans.answer.answer.strip as user_answer %}
                                    <style type="text/css">{{user_answer.1}}</style>
                                    <pre><code>{{user_answer.0|safe}}</code></pre>
                                  {% elif question.type == "mcc" or question.type == "mcq" %}
                                    {% for testcases in question.get_test_cases %}
                                      {% if testcases.id|stringformat:"i" in ans.answer.answer.strip %}
                                       <li>{{ testcases.options.strip|safe }}</li>
                                      {% endif %}
                                    {% endfor %}
                                  {% elif question.type == "arrange"%}
                                    {% get_answer_for_arrange_options ans.answer.answer question as tc_list %}
                                    {% for testcases in tc_list %}
                                     <li>{{ testcases.options.strip }}</li>
                                    {% endfor %}
                                  {% else %}
                                    {{ ans.answer.answer.strip }}
                                  {% endif %}
                                </div>
                              </div>
                            </div>
                          </div>
                          <br>
                        {% endfor %} <!-- End for ans in answers -->
                      </div>
                    </div>
                    {% endif %}
                    <br>
                    <div class="form-group">
                      <div class="col-md-2">
                        <label class="col-form-label" for="q{{ question.id }}">Marks:</label>
                        {% with answers|last as answer %}
                        <input id="q{{ question.id }}" type="text" name="q{{ question.id }}_marks" size="4" class="form-control" value="{{ answer.answer.marks }}"><br><br>
                        {% endwith %}
                      </div>
                    </div>
                    <hr/>
                    {% endfor %} {# for question, answers ... #}
                    <div class="form-group">
                      <h3>Teacher comments: </h3>
                      <textarea id="comments_{{paper.question_paper.id}}" class="form-control"
                        name="comments_{{ paper.question_paper.id }}">{{ paper.comments }}</textarea>
                      <br>
                      <button class="btn btn-success btn-lg" type="submit" name="submit_{{paper.quiz.id}}">
                        <i class="fa fa-save"></i>&nbsp;Save
                      </button>
                    </div>
                </form>
              </div>
            </div>
            {% endif %} {# if paper.answers.count #}
          {% endfor %} {# for paper in data.papers #}
        {% endif %} {#  if data.papers #}
      {% else %}
        <!-- No Data -->
      {% endif %} {#if data#}
    </div>
  </div>
</div>
{% endblock%}
